package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Level;
import com.hs.service.LevelService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName LevelController
 * @Description TODO
 * @Author qiuye
 * @Date 2025年08月01日 18:55
 * @Version 1.0
 */
@RestController
@RequestMapping("level")
public class LevelController {
    @Autowired
    private LevelService levelService;
//    增加
    @PostMapping
    public R addLevel(@RequestBody Level level){
        int i = levelService.addLevel(level);
        if (i == 0){
            return R.fail();
        }else {
            return R.ok();
        }
    }
//    修改
    @PutMapping
    public R updateLevel(@RequestBody Level level){
        int updateLevel = levelService.updateLevel(level);
        if (updateLevel == 0){
            return R.fail();
        }else {
            return R.ok();
        }
    }
//    删除
    @DeleteMapping("{id}")
    public R deleteLevel(@PathVariable("id") int id){
        int i = levelService.deleteLevel(id);
        if (i == 0){
            return R.fail();
        }else
            return R.ok();
    }
//    批量删除
    @DeleteMapping("ids")
    public R deleteLevels(int[] ids){
        int i = levelService.deletelIds(ids);
        if (i == 0){
            return R.fail();
        }else {
            return R.ok();
        }
    }
//    根据id查询
    @GetMapping("{id}")
    public R getLevel(@PathVariable("id") int id){
        Level level = levelService.getLevel(id);
        return R.ok(level);
    }
//    分页多条件
    @GetMapping("page")
    public R getLevels(Level level,
                       @RequestParam(required = false,defaultValue = "1") int page,
                       @RequestParam(required = false,defaultValue = "10")int size){
        PageInfo<Level> levels = levelService.getLevels(level, page, size);
        return R.ok(levels);
    }

}
